package com.wujunchao.demo.controller;

import com.wujunchao.demo.pojo.Admin;
import com.wujunchao.demo.pojo.Student;
import com.wujunchao.demo.pojo.Teacher;
import com.wujunchao.demo.service.impl.AdminServiceImpl;
import com.wujunchao.demo.service.impl.StudentServiceImpl;
import com.wujunchao.demo.service.impl.TeacherServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author :wn
 * @program : demo
 * @descript :
 * @create :2021-07-02 15:38
 */
@Controller
public class BaseController {

    @Resource
    StudentServiceImpl studentService;
    @Resource
    TeacherServiceImpl teacherService;
    @Resource
    AdminServiceImpl adminService;

    @RequestMapping("/login")
    public ModelAndView login(@Param("username") String username, @Param("password") String password, @Param("role") int role, HttpSession session) {
        ModelAndView error=new ModelAndView("/error");
        session.setAttribute("num",username);
        if (role == 0) {
            boolean login = studentService.login(username, password);
            ModelAndView md=new ModelAndView("/studentInformation");
            if (login) {
                md.addObject("username",username);
                return md;
            } else {

                return error;
            }
        } else if (role == 1) {
            boolean login = teacherService.login(username, password);
            ModelAndView md=new ModelAndView("/teacherInterface");
            if (login) {
                md.addObject("username",username);
                return md;
            } else {
                return error;
            }
        } else if (role == 2) {
            Admin login = adminService.login(username, password);
            if (login != null) {
                String arole = login.getArole();
                if (arole.equals("superAdmin")) {
                    ModelAndView md=new ModelAndView("/superAdminInterface");
                    md.addObject("username",username);
                    return md;
                } else {
                    ModelAndView md=new ModelAndView("/adminInterface");
                    md.addObject("username",username);
                    return md;
                }
            } else {
                return error;
            }
        } else {
            return error;
        }

    }
    @RequestMapping("/register")
    public String register(@Param("username")String username,@Param("passowrd")String password,@Param("enPassword")String enPassword,@Param("role")int role,@Param("name")String name){

        if (role==0){
            Student student=new Student();
            student.setSno(Integer.parseInt(username));
            student.setSname(name);
            student.setStatue("start");
            System.out.println(password);
            if (password.equals(enPassword)){
                student.setSpassword(password);
                studentService.xinZeng(student);
                return "login";
            }else {
                return "login";
            }
        }else if (role==1){
            Teacher teacher=new Teacher();
            teacher.setTno(Integer.parseInt(username));
            teacher.setTname(name);
            if (password.equals(enPassword)){
                teacher.setTpassword(password);
                teacherService.xinZeng(teacher);
                return "login";
            }else {
                return "error";
            }
        }else if(role==2){
            Admin admin=new Admin();
            admin.setAname(name);
            admin.setAno(Integer.parseInt(username));
            if (password.equals(enPassword)){
                admin.setApassword(password);
                adminService.xinZeng(admin);
                return "login";
            }else{
                return "login";
            }
        }else{
            return "login";
        }
    }
}
